<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Validate;
use think\Session;
use think\Cookie;
class Part extends Controller
{
    // 加载企业首页
    public function index()
    {
        return $this->fetch('company/index');
    }


    //显示职位管理页面
    public function Position()
    {
        // $name = input('post.name');
        $tel = Session::get('tel');
        if( empty($name) ){
            $num = mb_substr($tel,0,11);
            $result = Db::name('company')->field('company_id')->where('company_tel',$num)->find();
            $id = $result['company_id'];
            $data = Db::name('position')->where('position_companyid',$id)->paginate(10);
            $count = Db::name('position')->count();
        } else {
            $num = mb_substr($tel,0,11);
            $result = Db::name('company')->field('company_id')->where('company_tel',$num)->find();
            $id = $result['company_id'];
            $data = Db::name('position')->where('position_companyid',$id)->where('position_name','like',"%$name%")->paginate(10);
            $count = Db::name('position')->count();

        }

        $this->assign('count',$count);
        $this->assign('list',$data);
//        dump($data);die;
        return $this->fetch('index@part/position');
    }

    // 显示编辑页面
    public function positionEdit($id)
    {
        $data = Db::name('position')->find($id);

        $type = Db::name('category')->where('pid','1')->select();
        $att = Db::name('category')->where('pid','2')->select();
        $region = Db::name('category')->where('pid','3')->select();
//        dump($type); die;
        $this->assign('data',$data);
        $this->assign('type',$type);
        $this->assign('att',$att);
        $this->assign('region',$region);
        return $this->fetch('part/part-edit');
    }

    // 执行编辑
    public function partEdit($id)
    {

        $data = input('post.');
//        dump($data);
        $res['position_name'] = $data['name'];
        $res['position_start'] = strtotime($data['start']);
        $res['position_end'] = strtotime($data['end']);
        $res['position_num'] = $data['num'];
        $res['position_require'] = $data['require'];
        $res['position_tel'] = $data['phone'];
        $res['position_place'] = $data['place'];
        $res['position_cateid'] = $data['type'].','.$data['att'].','.$data['region'];
//        dump($res); die;


        $validate = new Validate([
            'phone' => ['^0{0,1}(13[0-9]|15[7-9]|153|156|18[7-9])[0-9]{8}$']
        ]);
        $phone = [
            'phone' => $data['phone']
        ];
        if (!$validate->check($phone)) {
            dump($validate->getError());
        }

        $result = Db::name('position')->where('position_id',$id)->update($res);

        if ($result == 0) {
            // return '<div style="font-size:50px;color:red;height:300px;width:300px;margin:100px auto;">'.$error['message'].'</div>';
            return $this->error($result);
        } else {
            return $this->success($result);
            // return '<div style="font-size:50px;color:red;height:300px;width:300px;margin:100px auto;">'.$error['message'].'</div>';
        }

    }

    //显示添加
    public function addPart()
    {
        $type = Db::name('category')->where('pid','1')->select();
        $att = Db::name('category')->where('pid','2')->select();
        $region = Db::name('category')->where('pid','3')->select();

        $this->assign('type',$type);
        $this->assign('att',$att);
        $this->assign('region',$region);
        return $this->fetch('part/part-add');
    }

    // 执行 添加 职位
    public function exeAdd()
    {
        $tel = Session::get('tel');
        $num = mb_substr($tel,0,11);
        $result = Db::name('company')->field('company_id')->where('company_tel',$num)->find();
        $id = $result['company_id'];
        $data = input('post.');
        $res['position_name'] = $data['name'];
        $res['position_start'] = strtotime($data['start']);
        $res['position_end'] = strtotime($data['end']);
        $res['position_num'] = $data['num'];
        $res['position_require'] = $data['require'];
        $res['position_tel'] = $data['phone'];
        $res['position_place'] = $data['place'];
        $res['position_cateid'] = $data['type'].','.$data['att'].','.$data['region'];
        $res['position_publish'] = time();
        $res['position_companyid'] = $id;
        $res['position_payway'] = $data['payway'];

        $validate = new Validate([
            'phone' => ['^0{0,1}(13[0-9]|15[7-9]|153|156|18[7-9])[0-9]{8}$']
        ]);
        $phone = [
            'phone' => $data['phone']
        ];
        if (!$validate->check($phone)) {
            dump($validate->getError());
        }

        $result = Db::name('position')->insert($res);

        if ($result == 0) {
            // return '<div style="font-size:50px;color:red;height:300px;width:300px;margin:100px auto;">'.$error['message'].'</div>';
            return $this->error($result);
        } else {
            return $this->success($result);
            // return '<div style="font-size:50px;color:red;height:300px;width:300px;margin:100px auto;">'.$error['message'].'</div>';
        }

    }

    //删除职位
    public function delPart($id)
    {
        $result = Db::name('position')->delete($id);
        if ($result > 0) {
//            $res = Db::name('position')->where('aid',$id)->delete($id);
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = 'ID为: ' . $id . '的职位删除成功!';
            return $this->success( 'ID为: ' . $id . '的职位删除成功!');
        } else {
            $info['status'] = false;
            $info['id'] = $id;
            $info['info'] = 'ID为: ' . $id . '的职位删除失败!';
            return $this->error('ID为: ' . $id . '的职位删除失败!');
        }

    }

    // 显示 兼职详情页面
    public function roleMessage($id)
    {

        $data = Db::name('position')->where('position_id',$id)->find();
//        dump($data); die;
        $data['position_start'] = date('Y-m-d',$data['position_start']);
        $data['position_end'] = date('Y-m-d',$data['position_end']);
        $data['position_publish'] = date('Y-m-d H:i:s',$data['position_publish']);

        $this->assign('list',$data);
        return $this->fetch('part/part-message');
    }

    // 查看职位发布的状态
    public function showStatus($status,$tel)
    {
        $num = mb_substr($tel,0,11);
        $result = Db::name('company')->field('company_id')->where('company_tel',$num)->find();
        $id = $result['company_id'];
        $data = Db::name('position')->where(["position_companyid"=>$id, "position_status"=>$status])->paginate(10);
        $count = Db::name('position')->count();
        $this->assign('count',$count);
        $this->assign('list',$data);
//        dump($data); die;
        if ( $status == 1 ){
            return $this->fetch('part/position1');
        } elseif( $status == 2 ){
            return $this->fetch('part/position2');
        } elseif( $status ==3 ) {
            return $this->fetch('part/position3');
        }


    }

}
